package com.yzy.soft.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.yzy.soft.common.persistence.dao.UserMapper;
import com.yzy.soft.common.persistence.model.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

@Controller
@RequestMapping("/")
public class IndexController {
    @Autowired
    private UserMapper userMapper;

    @GetMapping("")
    public ModelAndView hello(Map<String, Object> model1) {
        System.out.println("开始执行");
        ModelAndView model=new ModelAndView("/vipadmin/page/index");
        System.out.println("------------------------------------");
        System.out.println("------------------------------------");
        return model;
    }
    @GetMapping("index/2")
    public ModelAndView hello1(Map<String, Object> model1) {
        System.out.println("开始执行");
        ModelAndView model=new ModelAndView("/index");
        System.out.println("------------------------------------");
        System.out.println("------------------------------------");
        return model;
    }

    @GetMapping("index3")
    public ModelAndView hello2(Map<String, Object> model1) {
        System.out.println("开始执行");
        ModelAndView model=new ModelAndView("/index3");
        System.out.println("------------------------------------");
        System.out.println("------------------------------------");
        return model;
    }
    @GetMapping("login")
    public ModelAndView tologin(Map<String, Object> model1) {
        ModelAndView model=new ModelAndView();

            model.setViewName("/vipadmin/page/login");

        return model;
    }
    @PostMapping("login")
    public ModelAndView login(Map<String, Object> model1, @ModelAttribute User user) {
        ModelAndView model=new ModelAndView();
        Wrapper<User> userWrapper=new EntityWrapper<>();
        userWrapper.where("account={0} and password={1}",user.getAccount(),user.getPassword());
        Integer result=userMapper.selectCount(userWrapper);
        if(result==1){
            model.setViewName("redirect:/");
        }else{
            model.setViewName("/vipadmin/page/login");
        }
        return model;
    }
}
